package com.hntest.controller;

import com.hntest.entity.Admin;
import com.hntest.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller("loginContoller")
public class LoginController {

    @Autowired
    private AdminService adminService = null;

    public void setAdminService(AdminService adminService) {
        this.adminService = adminService;
    }

    @RequestMapping(value = "/doLogin",method = {RequestMethod.POST,RequestMethod.GET})
    public String doLogin(Admin admin, HttpServletRequest request){
        Admin ok = adminService.login(admin);
        if (ok!=null){
            HttpSession session = request.getSession(true);
            session.setAttribute("currUser",ok);
            String types = null;
            session.setAttribute("types","登录");
            return "redirect:/index.jsp";
        }else {
            return "forward:/login.jsp?msg=no";
        }
    }

    @RequestMapping(value = "/doReg",method = {RequestMethod.POST,RequestMethod.GET})
    public String doReg(Admin admin, HttpServletRequest request){
        boolean ok = adminService.register(admin);
        System.out.println("插入成功了吗？"+ok);
        if (ok){
            HttpSession session = request.getSession(true);
            session.setAttribute("currUser",ok);
            String types = null;
            session.setAttribute("types","注册");
            return "redirect:/index.jsp";
        }else {
            return "forward:/reg.jsp?msg=no";
        }
    }
}
